
# packages
library(estimatr); library(Rmisc); library(kableExtra); library(readr); library(here); library(tidyverse)

#directory
this_dir <- dirname(rstudioapi::getActiveDocumentContext()$path)

# load data
hire <- read_csv(here::here(this_dir, "hiring_data_all.csv"))

# search terms for questions about hill experience
search_patterns <- c(
  "hill experience",
  "congressional experience",
  "capitol hill",
  "member of congress",
  "government",
  "senate", "house", "connection", "relation"
)

# Use grep to search for patterns in each row of the text_data
matches <- grep(paste(search_patterns, collapse = "|"), 
                tolower(hire$Q139_1_TEXT), ignore.case = TRUE)

hire$ask_cong <- 0
hire$ask_cong[matches] <- 1 

# any political experience
hire$pol_exp <- ifelse(hire$app_prev_pol_exp_type == "None" | hire$app_prev_pol_exp_type == "Think Tank Director", 0, 1)

#collapse data at respondent level
hire_agg <- hire %>%
  mutate(ask_any = ifelse(is.na(Q139_1_TEXT) == T, 1, 0)) %>%
  group_by(ResponseId) %>%
  dplyr::summarise(ask_any = ifelse(any(ask_any == 1), 1, 0),
            ask_cong = ifelse(any(ask_cong == 1), 1, 0),
            all_pol = as.numeric(all(pol_exp == 1)), # no respondent is presented only with staffers
            pol_exp = ifelse(any(pol_exp == 1), 1, 0))

#estimate some models
ask_any_mod <- lm_robust(ask_any ~ pol_exp,
                 data = hire_agg)
summary(ask_any_mod)

ask_mod <- lm_robust(ask_cong ~ pol_exp,
              data = hire_agg)
summary(ask_mod)

ask_all_mod <- lm_robust(ask_cong ~ all_pol,
                     data = hire_agg)
summary(ask_all_mod)


# plot the difference between experimental groups
ask_df <- hire_agg %>%
  summarySE(
    measurevar = "ask_cong",
    groupvars = "pol_exp"
  )

p<- ggplot(ask_df, aes(x = pol_exp, y = ask_cong)) +
  geom_point() +
  theme_classic() +
  geom_errorbar(aes(ymin = ask_cong - 1.96*se,
                    ymax = ask_cong + 1.96*se), width = 0.015, color = "lightgrey") +
  geom_hline(yintercept = 0, lty = 2) +
  geom_errorbar(aes(ymin = ask_cong - 1.645*se,
                    ymax = ask_cong + 1.645*se), width = 0)  +
  annotate(geom = "text", x = 0.5, y = 0.15, label = paste0(round(coef(ask_mod)[2], digits = 2), "\n", "(",
                                                          round(sqrt(diag(vcov(ask_mod)))[2], digits = 2), ")")) +
  scale_x_continuous(limits = c(-0.5, 1.5),
                     breaks = c(0,1),
                     labels = c("No Profile w/\nBackground in Congress",
                                "Profile w/\nBackground in Congress")) +
  labs(x = NULL, y = "Probability of Enquiring about Personal Relationships")

ggsave(plot = p,
       filename = here::here(this_dir, "Figures", "open_ended_difference.pdf"),
       width = 5, height = 4)

# extract random examples from the treatment group
set.seed(99)
example_pol <- hire %>%
  distinct(ResponseId, .keep_all = T) %>% drop_na(Q139_1_TEXT) %>%
  group_by(pol_exp) %>%
  select(Q139_1_TEXT) %>%
  sample_n(5) %>%
  filter(pol_exp == 1)

no_hill <- filter(hire, pol_exp == 0) %>% distinct(ResponseId, .keep_all = T) %>% select(Q139_1_TEXT, pol_exp, ResponseId) %>% drop_na(Q139_1_TEXT)
no_hill <- no_hill[grep(pattern = "hill", x = tolower(no_hill$Q139_1_TEXT)),]

# combine with some selected examples from the control group
examples <- data.frame(no_pol_exp = c(no_hill$Q139_1_TEXT[c(13,6,9,16,30)]),
                       pol_exp = example_pol$Q139_1_TEXT)

names(examples) <- c("No Profile w/ Background in Congress", "At Least One Profile w/ Background in Congress")

# make latex table
column_widths <- c(3.5, 3.5) #fix column width

#table
ex_table <- kable(examples, format = "latex", booktabs = TRUE, caption = "Examples of Questions by Treatment Condition",
                  align = c("l", "l"))%>%
  kable_styling(full_width = F) %>%
  column_spec(1,width = "3in") %>%
  column_spec(2,width = "3in")

#export
writeLines(ex_table,
           here::here(this_dir, "Figures", "examples_open_ended_q.tex"))


